﻿namespace CN2P.ServiceModel.ServiceClients
{
    using System;
    using System.Collections.Generic;
    using System.Net;
    using System.Text;

    public class UserSessionServiceClient : BaseServiceClient
    {
        public UserSessionServiceClient(string baseUrl)
            : base(baseUrl)
        {
        }


        public void Post(string userId, SessionItemModel sessionItem)
        {
            this.Post(userId, Serialize(sessionItem));
        }

        public void Post(string userId, string sessionItemJson)
        {
            var url = this.GetSessionUrl();
            var response = this.ExecutePost(url, sessionItemJson);
            this.ValidateResponse(response, HttpStatusCode.Created);
        }

        public IList<SessionItemModel> Get(string userId, int count)
        {
            var url = this.GetSessionUrl(userId) + "?count=" + count;
            var response = this.ExecuteGet<List<SessionItemModel>>(url);
            this.ValidateResponse(response, HttpStatusCode.OK);

            return response.Content;
        }

        protected string GetSessionUrl(string userId)
        {
            var bytes = Encoding.UTF8.GetBytes(userId);
            var base64 = Convert.ToBase64String(bytes);
            var encoded = Uri.EscapeUriString(base64);
            return this.GetSessionUrl() + encoded;
        }

        protected string GetSessionUrl()
        {
            return _baseUrl + "api/vieweditems/";
        }
    }
}
